package com.voice.call.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.maxmind.geoip2.model.CountryResponse;
import com.voice.call.common.Const;
import com.voice.call.entity.SupportMcc;
import com.voice.call.mapper.SupportMccMapper;
import com.voice.call.service.IpQueryService;
import com.voice.call.service.SupportMccService;
import com.voice.call.util.entity.SupportRegister;
import com.voice.call.utils.StringUtil;

@Service
public class SupportMccServiceImpl implements SupportMccService{
	
	@Resource
	private IpQueryService ipQueryService;
	
	@Resource
	private SupportMccMapper supportMccMapper;
	
	
	@Override
	public int getSupportLoginBindPhoneCount(String phone){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("category", Const.SUPPORT_CATEGORY_IS_REGISTER);
		params.put("areaCode", phone);
		
		return supportMccMapper.getSupportMccPhoneCount(params);
	}

	@Override
	public boolean isSupportRegister(String ip,String mcc){
		String code = ipQueryService.getIsoCodeForIp(ip);
		
		if (code==null) {
			return false;
		}
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("category", Const.SUPPORT_CATEGORY_IS_REGISTER);
		if (!StringUtil.isEmpty(code)) {
			params.put("isoCode", code);
		}
		if (!StringUtil.isEmpty(mcc)) {
			params.put("mcc", mcc);
		}
		int total = supportMccMapper.count(params);
		if (total > 0) {
			return true;
		}
		return false;
		
	}
	
	@Override
	public boolean isSupportCountry(String ip){
       return this.isSupportRegister(ip, null);
	}
	
	@Override
	public SupportRegister handlerSupportRegister(String ip,String mcc){
		
		SupportRegister supportRegister = new SupportRegister();
		
		CountryResponse rsp=ipQueryService.getIpFoResponse(ip);
        //String code = ipQueryService.getIsoCodeForIp(ip);
        //supportRegister.setIsoCode(code);
        supportRegister.setSupport(false);
		if (rsp==null) {
			return supportRegister;
		}
		supportRegister.setMcc(rsp.getCountry().getIsoCode());
		supportRegister.setCountry(rsp.getCountry().getName());
		String code = rsp.getCountry().getIsoCode();
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("category", Const.SUPPORT_CATEGORY_IS_REGISTER);
		if (!StringUtil.isEmpty(code)) {
			params.put("isoCode", code);
		}
		if (!StringUtil.isEmpty(mcc)) {
			params.put("mcc", mcc);
		}
		List<SupportMcc> list = supportMccMapper.list(params);
		
		if (list!=null && !list.isEmpty()) {
			supportRegister.setIsoCode(list.get(0).getAreaCode());
			supportRegister.setSupport(true);
		}
		/**
		int total = supportMccMapper.count(params);
		if (total > 0) {
			supportRegister.setSupport(true);
		}
		**/
		return supportRegister;
		
	}
	
	//获取某个类别的积分
	@Override
	public	SupportMcc getSupportCategoryIntegral(String category,String ip){
			
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("category", category); //
		params.put("code", Const.SUPPORT_CATEGORY_ISOCODE);
		String code = ipQueryService.getIsoCodeForIp(ip);
		if (!StringUtil.isEmpty(code)) {
			params.put("isoCode", code); //
		}
		List<SupportMcc> list = supportMccMapper.queryForCategory(params);
		if (list!=null && !list.isEmpty()) {
			SupportMcc entity = null;
			for (SupportMcc supportMcc : list) {
				
				if (supportMcc.getIsoCode().equals(Const.SUPPORT_CATEGORY_ISOCODE)) {
					entity = supportMcc;
				}
				
				if (!supportMcc.getIsoCode().equals(Const.SUPPORT_CATEGORY_ISOCODE)) {
					return supportMcc;
				}
				
			}
			return entity;
			
		}
		return null;
	}
}
